package appletOne;


import globalCode.PolicyHandler;
import globalCode.UserConnection;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;



public class UCTestApplet extends Applet{
	public class InputPanel extends JPanel{
		public class TextFieldPanel extends JPanel{
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
			private JTextField idField1;
			private JTextField idField2;

			public TextFieldPanel(){

				this.setLayout(new GridLayout(2, 2));
				JLabel idField1Label = new JLabel();
				idField1Label.setText("User ID 1");
				this.add(idField1Label);
				idField1 = new JTextField();
				this.add(idField1);

				JLabel idField2Label = new JLabel();
				idField2Label.setText("User ID 2");
				this.add(idField2Label);
				idField2 = new JTextField();
				this.add(idField2);

			}

			public String[] getInput() {
				String[] toReturn = new String[2];
				toReturn[0] = idField1.getText();
				toReturn[1] = idField2.getText();
				return toReturn;
			}
		}
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		UCTestApplet applet;
		private TextFieldPanel tFP;

		public InputPanel(UCTestApplet ma){
			applet = ma;

			this.setLayout(new BorderLayout());
			JButton computeButton = new JButton();
			computeButton.addMouseListener(new MouseListener(){
				@Override
				public void mouseClicked(MouseEvent arg0) {
					applet.compute();
				}
				@Override
				public void mouseEntered(MouseEvent arg0) {}
				@Override
				public void mouseExited(MouseEvent arg0) {}
				@Override
				public void mousePressed(MouseEvent arg0) {}
				@Override
				public void mouseReleased(MouseEvent arg0) {}
			});
			computeButton.setText("Compute");
			this.add(computeButton, BorderLayout.SOUTH);

			tFP = new TextFieldPanel();
			this.add(tFP, BorderLayout.NORTH);
		}

		public String[] getInput() {
			return tFP.getInput();
		}
	}
	public class OutputPanel extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private JTextArea outputArea;

		public OutputPanel(){
			this.setLayout(new GridLayout(2, 1));
			JLabel outputLabel = new JLabel();
			outputLabel.setText("Output");
			this.add(outputLabel);
			
			outputArea = new JTextArea();
			JScrollPane scrollPane = new JScrollPane(outputArea);
			//outputArea.setEnabled(false);
			//outputArea.setPreferredSize(new Dimension(100, 400));
			this.add(scrollPane);
		}

		public void print(String message) {
			outputArea.setText(message);
			repaint();
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	InputPanel input;

	OutputPanel output;
	public void compute() {

		String[] inputStrs = input.getInput();
		int user1 = 0;
		int user2 = 0;
		try{
			user1 = Integer.parseInt(inputStrs[0]);
			user2 = Integer.parseInt(inputStrs[1]);
		}catch(NumberFormatException ex){
			output.print("Invalid Input");
			return;
		}

		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			output.print(e.getMessage());
			return;
		}

		//String url = "jdbc:mysql://ischooldev.fcps.net";
		String url = "jdbc:mysql://dosshome.dyndns-office.com:63306";
		//String url = "jdbc:mysql://192.168.1.33:3306";
		String username = "root";
		String password = "PAss33word!!";
		Connection con = null;
		
		try {
			con = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			output.print(e.getMessage());
			return;
		}
		
		
		
		UserConnection uc = new UserConnection(user1, user2, con);
		output.print(uc.toString());
	}

	@Override
	public void start(){
		this.setSize(600, 400);
		this.setVisible(true);
		this.setLayout(new GridLayout(1,2));
		input = new InputPanel(this);
		this.add(input);
		output = new OutputPanel();
		this.add(output);
		
		PolicyHandler ph = new PolicyHandler();
		output.print(ph.setPolicies());
	}

	@Override
	public void stop(){
		System.exit(0);
	}


}
